package com.GlobalPaint.app.ui.Home.friend;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.GlobalPaint.app.Adapter.tianjiaFriendsAdapter;
import com.GlobalPaint.app.R;
import com.GlobalPaint.app.bean.EventBusben;
import com.GlobalPaint.app.bean.MyFriendBean;
import com.GlobalPaint.app.bean.ReturnStatusResultEntity;
import com.GlobalPaint.app.bean.gethaoyouBean;
import com.GlobalPaint.app.utils.CommUtility;
import com.GlobalPaint.app.utils.Constants;
import com.GlobalPaint.app.utils.DataManager;
import com.blankj.utilcode.utils.RegexUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class popupActivity extends AppCompatActivity {
    private gethaoyouBean bean;
    private MyFriendBean myFriendBean;
    private List<gethaoyouBean.DataBean>list;
    @BindView(R.id.iv_goback)
    ImageView ivGoback;
    @BindView(R.id.al_et_sch)
    EditText alEtSch;
    @BindView(R.id.al_seach)
    TextView alSeach;
    @BindView(R.id.al_recy)
    RecyclerView alRecy;
    private LinearLayoutManager linearLayoutManager;
    private tianjiaFriendsAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_popup);
        ButterKnife.bind(this);
        linearLayoutManager =new LinearLayoutManager(popupActivity.this);
        alRecy.setLayoutManager(linearLayoutManager);

    }
    private void getHaoyou() {
        String id= alEtSch.getText().toString();
        if(id.isEmpty()){
            Toast.makeText(popupActivity.this, "请输入好友名称", Toast.LENGTH_LONG).show();
            return;
        }
        if (!RegexUtils.isMobileSimple(id)) {
            CommUtility.ShowMsgShort(this, "请输入正确手机号");
            return;
        }
        String url = Constants.JASON_SERVICE_URL + "user/searchUser?userName=" + id;
        DataManager.getInst().getHttpRequestJsonClass(url, DataManager.userEntity.getCookie(), gethaoyouBean.class, new DataManager.Callback() {
            @Override
            public void onSuccess(Object o) {
                bean = (gethaoyouBean) o;
                if (bean.getStatus() == 1) {
                    if (bean.getData() != null ) {
                        adapter = new tianjiaFriendsAdapter(bean.getData(),popupActivity.this);
                        alRecy.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                    }else {
                        Toast.makeText(popupActivity.this, "暂无此好友", Toast.LENGTH_LONG).show();
                    }
                }
            }

            @Override
            public void onFailure(ReturnStatusResultEntity error) {


            }
        });
    }
    public void tianjiaHaoyou(String userName, String jid, String nick){

        String url = Constants.JASON_SERVICE_URL + "appFriends/addAttention?userName=" + userName+
                "&jid="+jid+"&nick="+nick;
        DataManager.getInst().getHttpRequestJsonClass(url, DataManager.userEntity.getCookie(), MyFriendBean.class, new DataManager.Callback() {
            @Override
            public void onSuccess(Object o) {
                myFriendBean = (MyFriendBean) o;
                if (myFriendBean.getStatus() == 1) {
                    EventBusben baseMessage = new EventBusben("guanzhu");
                    EventBus.getDefault().post(baseMessage);
                    Toast.makeText(popupActivity.this, "添加成功", Toast.LENGTH_LONG).show();
                    finish();
                } else {
                    Toast.makeText(popupActivity.this, myFriendBean.getMsg(), Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(ReturnStatusResultEntity error) {


            }
        });
    }

    @OnClick({R.id.iv_goback, R.id.al_seach})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_goback:
                finish();
                break;
            case R.id.al_seach:
                getHaoyou();
                break;
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();

    }
}
